/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;
import com.jaspersoft.jrx.export.JRTxtExporter;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRPptxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.view.JasperViewer;
import org.hibernate.HibernateException;
import org.hibernate.Session;
/**
 *
 * @author gadiel
 */
public class Printer {
    public static final String PDF = "pdf", HTML = "html", CSV = "csv", 
            RTF = "rtf", XLS = "xls", XML = "xml", TXT = "txt", XLSX = "xlsx",
            DOCX = "docx", PPTX = "pptx";
    public static void verReporte(String nombreReporte, Map parametros)
    {
        try {
            // Fill the report using an empty data source
            Session se = prestar.mapeos.HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tr = se.beginTransaction();
            JasperPrint print = JasperFillManager.fillReport(nombreReporte, parametros, se.connection());
            tr.commit();
            JasperViewer.viewReport(print, false);
        } 
        catch (HibernateException | JRException e) {
        }
    }
    public static void verReporte(String nombreReporte, Map parametros, JRDataSource dataSource)
    {
        try {
            // Fill the report using an empty data source
            JasperPrint print = JasperFillManager.fillReport(nombreReporte, parametros, dataSource);
            JasperViewer.viewReport(print, false);
        } 
        catch (Exception e) {
        }
    }
    public static void exportarReporte(String nombreReporte, Map parametros, String archivo, String formato)
    {
        try {
            // Fill the report using an empty data source
            Session se = prestar.mapeos.HibernateUtil.getSessionFactory().getCurrentSession();
            org.hibernate.Transaction tr = se.beginTransaction();
            JasperPrint print = JasperFillManager.fillReport(nombreReporte, parametros, se.connection());
            tr.commit();
            JRExporter exporter = null;
            switch (formato) {
                case Printer.PDF:
                    exporter = new JRPdfExporter();
                    break;
                case Printer.HTML:
                    exporter = new JRHtmlExporter();
                    break;
                case Printer.CSV:
                    exporter = new JRCsvExporter();
                    break;
                case Printer.RTF:
                    exporter = new JRRtfExporter();
                    break;
                case Printer.XLS:
                    exporter = new JRXlsExporter();
                    break;
                case Printer.XML:
                    exporter = new JRXmlExporter();
                    break;
                case Printer.TXT:
                    exporter = new JRTxtExporter();
                    break;
                case Printer.XLSX:
                    exporter = new JRXlsxExporter();
                    break;
                case Printer.DOCX:
                    exporter = new JRDocxExporter();
                    break;
                case Printer.PPTX:
                    exporter = new JRPptxExporter();
                    break;
            }
            
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE,new java.io.File(archivo + formato));
            exporter.exportReport();
        } 
        catch (HibernateException | JRException e) {
        }
    }    
}
